﻿Public Class ClassQuotation
    Public Shared Function addQuotation(ByVal quotcode As String, ByVal empid As Integer, ByVal ship As Double, ByVal amount As Double, ByVal discount As Double, ByVal vat As Double, ByVal total As Double, ByVal dgv As DataGridView, ByVal status As Int16, ByVal pay As Double)
        Dim sql1 = <sql>
                    insert into tbl_quotation(quot_code,date,employee_id,shipping,amount,discount,vat,total,order,active)
                    value(@code,@date,@emp,@ship,@amount,@discount,@vat,@date,@total,0,1)
                  </sql>
        Dim sql3 = <sql>
                    insert into tbl_quotation_payment(quot_id,date,amount,status)
                    value(@quid,@date,@amount,@status)
                 </sql>
        Dim sql2 = <sql>
                    insert into tbl_quotation_detail(quot_id,item_id,qty,price,discount,cost)
                    value(@quid,@item,@qty,@price,@disc,@cost) 
                 </sql>

        Try
            Cmd.Begin()
            Cmd.Parameters("@code") = quotcode
            Cmd.Parameters("@date") = Now.Date
            Cmd.Parameters("@emp") = empid
            Cmd.Parameters("@ship") = ship
            Cmd.Parameters("@amount") = amount
            Cmd.Parameters("@discount") = discount
            Cmd.Parameters("@vat") = vat
            Cmd.Parameters("@total") = total
            Cmd.ExecuteNonQuery(sql1)

            Dim quid = Cmd.ExecuteScalar("select MAX(quot_id) from tbl_quotation")



            Cmd.Parameters("@quid") = quid
            Cmd.Parameters("@date") = Now.Date
            Cmd.Parameters("@amount") = pay
            Cmd.Parameters("@status") = status
            Cmd.ExecuteNonQuery(sql3)

            For i = 0 To dgv.Rows.Count - 1
                Cmd.Parameters("@quid") = quid
                Cmd.Parameters("@item") = dgv.Rows(i).Cells("item_id").Value
                Cmd.Parameters("@qty") = dgv.Rows(i).Cells("item_qty").Value
                Cmd.Parameters("@price") = dgv.Rows(i).Cells("item_price").Value
                Cmd.Parameters("@disc") = dgv.Rows(i).Cells("item_discount").Value
                Cmd.Parameters("@cost") = dgv.Rows(i).Cells("amount").Value
                Cmd.ExecuteNonQuery(sql2)
            Next

            myMsgbox.show("Add New Quotation Success!!!", "New Quotation")
            Cmd.Commit()
            Return True
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
            Cmd.Rollback()
            Return False
        End Try
    End Function
End Class
